#
# Copyright 2008 Wepoco.  http://www.wepoco.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from GPXTimeline import GPXTimeline

from template import short_header, banner, footer
from mapskey import MAPS_API_KEY


class Map(webapp.RequestHandler):
  def get(self):
    key = self.request.get('key')
    track = db.get(db.Key(key))

    timeline = GPXTimeline().timeline(track.getContent())
    (wpt_a, wpt_b) = timeline[0] 
    self.start_lat = wpt_a.lat
    self.start_lon = wpt_a.lon

    # The Google Maps API GGeoXML() call will
    # fetch the KML file.
    self.kml_url = self.request.host_url + "/download?key=" + key
    self.kml_url2 = self.request.host_url + "/kml?key=" + key

    self.response.out.write(short_header(self.request))
    self.draw_map3D()
    self.response.out.write(footer(self.request))
    return
    
  def draw_map3D(self):
    kml_url = self.kml_url
    kml_url2 = self.kml_url2
    start_lat = self.start_lat
    start_lon = self.start_lon
    self.response.out.write("""<script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=%s"
      type="text/javascript"></script>""" % MAPS_API_KEY)

    self.response.out.write("""<script type="text/javascript"> 
 //<![CDATA[
    var map;
    var geoXml; 
    var geoXml2; 
    var toggleState = 1;
    function kmlLoaded() {
      geoXml.gotoDefaultViewport(map);
    }
    function initialize() {
      if (GBrowserIsCompatible()) {
        geoXml = new GGeoXml("%s", kmlLoaded);
        geoXml2 = new GGeoXml("%s");
        map = new GMap2(document.getElementById("map_canvas")); 
        map.setCenter(new GLatLng(%s,%s), 11); 
        map.addMapType(G_SATELLITE_3D_MAP);

        map.addControl(new GHierarchicalMapTypeControl());
        map.addControl(new GLargeMapControl());
        map.addOverlay(geoXml);
        map.addOverlay(geoXml2);
      }
    }""" % (kml_url, kml_url2, start_lat, start_lon))

    self.response.out.write("""
    function toggleMyKml() {
      if (toggleState == 1) {
        map.removeOverlay(geoXml);
        toggleState = 0;
      } else {
        map.addOverlay(geoXml);
        toggleState = 1;
      }
    }
 //]]>""")

    self.response.out.write("""
</script></head>
<body onload="initialize()">""")

    self.response.out.write(banner(self.request))

    self.response.out.write("""

    <div id="map_canvas" style="width: 640px; height: 480px; float:left; border: 1px solid black;">
    </div>
    <br clear="all"/>
    <br/>
""")
    
